﻿namespace _24_hour_blog.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using WebMatrix.WebData;

    public class InitSecurityDb
    {
        public void Seed()
        {
            WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("SecurityConnection", "User", "UserId", "Username", autoCreateTables: true);
            var roles = (WebMatrix.WebData.SimpleRoleProvider)Roles.Provider;
            var membership = (WebMatrix.WebData.SimpleMembershipProvider)Membership.Provider;
            if (!roles.RoleExists("Administrator"))
            {
                roles.CreateRole("Administrator");
            }

            if (!roles.RoleExists("Contributor"))
            {
                roles.CreateRole("Contributor");
            }

            if (!roles.RoleExists("Guest"))
            {
                roles.CreateRole("Guest");
            }

            if (!WebSecurity.UserExists("sjf"))
            {
                WebSecurity.CreateUserAndAccount("sjf", "password", new { FirstName = "Scott", LastName = "Foster", Email = "jfoster84@live.com" }, false);
            }

            if (!roles.GetRolesForUser("sjf").Contains("Administrator"))
            {
                roles.AddUsersToRoles(new[] { "sjf" }, new[] { "Administrator" });
            }

            if (!roles.GetRolesForUser("sjf").Contains("Contributor"))
            {
                roles.AddUsersToRoles(new[] { "sjf" }, new[] { "Contributor" });
            }

            if (!roles.GetRolesForUser("sjf").Contains("Guest"))
            {
                roles.AddUsersToRoles(new[] { "sjf" }, new[] { "Guest" });
            }
        }
    }
}
